$(function(){ 

    //var log = console.log; 
    var $content = $('#content');  

    //The Form itself
    $content.find('form').wl_Form({
        submitButton: 'button.submit',
        resetButton: 'button.reset',
        method: 'post',
        action: null,
        ajax: true,
        serialize: false,
        parseQuery: true,
        dataType: 'text',
        status: true,
        sent: false,
        confirmSend: true,
        text: {
            required: 'Ovo polje je obavezno',
            valid: 'Ovo polje nije u redu',
            password: 'Lozinka je kratka',
            passwordmatch: 'Ovaa lozinka se ne poklapa',
            fileinqueue: 'Postoji najmanje jedan fajl na čekanju',
            incomplete: 'Molimo popunite formu ispravno!',
            send: 'pošalji formu...',
            sendagain: 'pošalji ponovo?',
            success: 'forma poslata!',
            error: 'greška prilikom slanja!',
            parseerror: 'Nemogu da deserializujem query string:\n %e'
        },
        tooltip: {
            gravity: 'nw'
        },
        onRequireError: function (element) {},
        onValidError: function (element) {},
        onPasswordError: function (element) {},
        onFileError: function (element) {},
        onBeforePrepare: function () {

            var chk = $('#directory').val();

            if(chk==0){

                $('#srvrserror-3').html('molimo odaberite direktorijum');
                $('#srvrserror-3').show('slow');

            }else {
                loader();
                
                parent = $("#directory").val(); 

                $.ajax({
                    type: "POST",
                    url: base_url+'directoryMedia/edit_insert',
                    data: { 
                        id: get_id,   
                        parent: parent,   
                        name: $("#name").val(),
                        description: $("#description").val(),
                        appmode: appmode
                    },
                    dataType: 'json',
                    success: function(data){

                        if(data.action==true){

                            $.modal.close();
                            /*----------------------------------------------------------------------*/
                            /* go to preview article url 
                            /*----------------------------------------------------------------------*/
                            document.location.href = base_url + 'directoryMedia/preview';

                        }else{

                            alert('Molim odaberte sva polja.'+data.msg)

                        }


                    }
                })
            }

            return false;
        },
        onBeforeSubmit: function (data) {},
        onReset: function () {},
        onComplete: function (textStatus, jqXHR) {},
        onError: function (textStatus, error, jqXHR) {},
        onSuccess: function (data, textStatus, jqXHR) {}
    }); 

    /***********************************************************
    * LOADERS
    ***********************************************************/    

    function loader()
    {
        $.modal(
        "<div><div class='animate'><img src='"+base_url+"assets/img/backgnds/loading.gif'></div><h2>Molim sačekajte...</h2></div>" , {
            closeHTML: "",
            containerCss:{
                height:70,
                width:120,
                borderColor:"#dedede"
            },
            opacity:20,
            overlayCss: {
                backgroundColor: "#000000"
            }
        });
    }

});
